package database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import databaseInterface.CustomerManageInterface;
import databaseInterface.UserManageInterface;

import po.StatusEnum;
import po.UserPO;

public class UserManage implements UserManageInterface{
	Statement stmt;
	DatabaseConnection db= new DatabaseConnection();
	
	public void connect (){
        
         stmt =db.connect();
    }
    
	public void insert (UserPO user){
    	 String insert ="insert into user values ('"+user.getName()+"','"+user.getPassword()+"','"+Format.toString(user.getStatus())+"')";
         try {
			stmt.executeUpdate(insert);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    
   }

	public  void delete (String name ){
    	
    	String delete="delete from user where userName = '"+name+"'" ;
    	try {
			stmt.executeUpdate(delete);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
	public void update(UserPO user,String name){
    	
    	String update ="update user set userName ='"+user.getName()+"', password = '"+user.getPassword()+"', status = '"+Format.toString(user.getStatus())+"' where userName = '"+name+"'" ;
        try {
			stmt.executeUpdate(update);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}    
    }
    
	public ArrayList<UserPO> query (String name ){
    	String query = "select * from user where userName = '"+name+"'";
    	ArrayList<UserPO> userlist = new ArrayList<UserPO> ();
     	try {
			ResultSet rs= stmt.executeQuery(query);
		
    	
    	while(rs.next()){
    	    UserPO user= toUserPO(rs);
    	    userlist.add(user);
    	
    	}
    	} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	return userlist;	
    }
  
  
	public ArrayList<UserPO> query ( ){
  	String query = "select * from user ";
  	ArrayList<UserPO> userlist = new ArrayList<UserPO> ();
   	try {
			ResultSet rs= stmt.executeQuery(query);
		
  	
  	while(rs.next()){
  	    UserPO user= toUserPO(rs);
  	    userlist.add(user);
  	
  	}
  	} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
  	
  	return userlist;	
  }
    private UserPO toUserPO(ResultSet rs) {
	// TODO Auto-generated method stub
    	UserPO user = new UserPO();
    	try {
			 user.setName(rs.getString(1));
			 user.setPassword(rs.getString(2));
			 user.setStatus(toStatusEnum(rs.getString(3)));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	return user;
}
    
	private StatusEnum toStatusEnum(String str) {
		// TODO Auto-generated method stub
			if(str.equalsIgnoreCase("customer"))  return StatusEnum.Customer;
			else if(str.equalsIgnoreCase("manager"))  return StatusEnum.Manager;
			else if(str.equalsIgnoreCase("saleManager")) return StatusEnum.SaleManager;
			else return StatusEnum.SystemManager;
	 		
		}
		
	

	public void close(){
      db.close();
    }
   

}
